package com.gec.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.model.entity.SysReport;
import com.gec.model.vo.SysReportQueryVo;
import com.gec.system.service.SysReportService;
import com.gec.system.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 公告表 前端控制器
 * </p>
 *
 * @author lxj
 * @since 2024-03-07
 */
@Api(tags = "通告管理控制器")
@RestController
@RequestMapping("/admin/system/sysReport")
public class SysReportController {

    @Autowired
    private SysReportService sysReportService;

    @ApiOperation("分页查询列表")
    @GetMapping("/{page}/{limit}")
    public Result findPage(@PathVariable Long page,
                           @PathVariable Long limit,
                           SysReportQueryVo vo){

        IPage<SysReport> page1 = new Page<>(page,limit);
        page1 = this.sysReportService.selectPage(page1,vo);
        return Result.ok(page1);
    }

    //    新增
    @ApiOperation("新增信息")
    @PostMapping("/addReport")
    public Result addReport(@RequestBody SysReport sysReport) {
        boolean res = this.sysReportService.save(sysReport);
        if (res) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    // 修改
    //1.根据id 去得到当前分类
    @ApiOperation("根据id获取信息")
    @GetMapping("/findReportById/{id}")
    public Result findReportById(@PathVariable Long id)
    {
        SysReport sysReport = this.sysReportService.getById(id);
        return Result.ok(sysReport);
    }

    //    增加阅读量
    @ApiOperation("根据id获取信息")
    @GetMapping("/addReportViewsById/{id}")
    public Result addReportViewsById(@PathVariable Long id)
    {
        boolean b = this.sysReportService.addViews(id);
        if (b){
            return Result.ok();
        }
        return Result.fail();
    }

    // 实现修改
    @ApiOperation("修改信息")
    @PostMapping("/updateReport")
    public Result updateReport(@RequestBody SysReport sysReport)
    {
        boolean b = this.sysReportService.updateById(sysReport);
        if (b)
        {
            return Result.ok();
        }
        else
        {
            return Result.fail();
        }
    }

    //    根据单个id 删除数据
    @ApiOperation("根据id删除")
    @DeleteMapping("/removeReportById/{id}")
    public Result removeReportById(@PathVariable Long id){

        boolean b = this.sysReportService.removeById(id);
        if (b){
            return Result.ok();
        }
        return Result.fail();
    }

    // 批量删除
    @ApiOperation("批量删除信息")
    @DeleteMapping("/removeReportByIds")
    public Result removeReportByIds(@RequestBody List<Long> ids)
    {
        boolean b = this.sysReportService.removeByIds(ids);

        if (b)
        {
            return Result.ok();
        }
        else
        {
            return Result.fail();
        }
    }


}

